package io.renren.modules.diycake.service.impl;

import io.renren.modules.diycake.entity.MaterialEntity;
import io.renren.modules.diycake.service.UserService;
import io.renren.modules.diycake.vo.CakeVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import io.renren.modules.diycake.dao.CakeDao;
import io.renren.modules.diycake.entity.CakeEntity;
import io.renren.modules.diycake.service.CakeService;


@Service("cakeService")
public class CakeServiceImpl extends ServiceImpl<CakeDao, CakeEntity> implements CakeService {
@Autowired
private UserService userService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<CakeEntity> wrapper = new QueryWrapper<>();
        String key = ""+params.get("key");
        wrapper.like("name",key);

        IPage<CakeEntity> page = this.page(
                new Query<CakeEntity>().getPage(params),
                wrapper
        );

        List<CakeVo> collect = page.getRecords().stream().map(item -> {
            CakeVo cakeVo = new CakeVo();
            BeanUtils.copyProperties(item, cakeVo);
            cakeVo.setUserName(userService.getById(item.getUserId()).getName());
            return cakeVo;
        }).collect(Collectors.toList());
        PageUtils pageUtils = new PageUtils(page);
        pageUtils.setList(collect);
        return pageUtils;
    }

}